/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.domain.model.LoginUser;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.core.validate.EditGroup;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.*;
import com.comac.ins.isfd.domain.bo.query.IsfdModelManagementPartQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementConfirmImportPartVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementPartOuterVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementPartVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementStructureDefinitionTreeNodeVo;
import com.comac.ins.isfd.service.IIsfdModelManagementPartService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 模型管理-零件controller
 *
 * @author Lion Li
 * @date 2024-08-14
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/modelManagementPart")
public class IsfdModelManagementPartController extends BaseController {

    @Autowired
    private IIsfdModelManagementPartService isfdModelManagementPartService;


    /**
     * 根据部段编码查询组件
     */
    @SaCheckPermission("isfd:modelManagementPart:getComponentBySection")
    @GetMapping("/getComponentBySection")
    public R<List<String>> getComponentBySection(@RequestParam("modelNumber") String modelNumber,
                                                 @RequestParam("batchNumber") String batchNumber,
                                                 @RequestParam("sectionNumber") String sectionNumber,
                                                 @RequestParam(value = "necessaryItemsFlag", required = false) Boolean necessaryItemsFlag) {
        return R.ok(isfdModelManagementPartService.getComponentBySection(modelNumber, batchNumber, sectionNumber, necessaryItemsFlag));
    }

    /**
     * 选零件
     */
    @SaCheckPermission("isfd:modelManagementPart:selectPart")
    @PostMapping("/selectPart")
    public R<List<IsfdModelManagementPartOuterVo>> selectPart(@RequestBody List<IsfdModelManagementSelectPartBo> selectList) {
        return R.ok(isfdModelManagementPartService.selectPart(selectList));
    }

    /**
     * 模型管理左侧树查询
     */
    @SaCheckPermission("isfd:modelManagementPart:tree")
    @GetMapping("/listTree")
    public R<List<IsfdModelManagementStructureDefinitionTreeNodeVo>> listTree() {
        return R.ok(isfdModelManagementPartService.listTree());
    }

    /**
     * 模型管理零件分页列表查询
     */
    @SaCheckPermission("isfd:modelManagementPart:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdModelManagementPartOuterVo> list(@RequestBody IsfdModelManagementPartQueryBo bo) {
        return isfdModelManagementPartService.queryPageList(bo);
    }

    /**
     * 零件查看详情
     */
    @SaCheckPermission("isfd:modelManagementPart:detail")
    @GetMapping("/detail")
    public R<IsfdModelManagementPartOuterVo> detail(@NotEmpty(message = "机型") @RequestParam("modelNumber") String modelNumber,
                                                    @NotEmpty(message = "批次号") @RequestParam("batchNumber") String batchNumber,
                                                    @NotEmpty(message = "部段号") @RequestParam("sectionNumber") String sectionNumber,
                                                    @NotEmpty(message = "组建号") @RequestParam("componentNumber") String componentNumber,
                                                    @NotEmpty(message = "零件号") @RequestParam("partNumber") String partNumber) {
        return R.ok(isfdModelManagementPartService.detail(modelNumber, batchNumber, sectionNumber, componentNumber, partNumber));
    }

    /**
     * 整机导入-规则检查
     */
    @SaCheckPermission("isfd:modelManagementPart:check:import")
    @Log(title = "整机导入-规则检查", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/check/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<List<IsfdModelManagementPartVo>> checkImport(@RequestParam("file") MultipartFile file,
                                                          @NotEmpty(message = "所选机型编号不能为空") @RequestParam("modelNumber") String modelNumber,
                                                          @NotEmpty(message = "所选批次编号不能为空") @RequestParam("batchNumber") String batchNumber,
                                                          @RequestParam(value = "indexFileName", required = false) String indexFileName) {
        List<IsfdModelManagementPartVo> voList = isfdModelManagementPartService.checkImport(file, modelNumber, batchNumber, indexFileName);
        return R.ok(voList);
    }


    /**
     * 修改零组件号
     */
    @SaCheckPermission("isfd:modelManagementPart:update")
    @Log(title = "修改零组件号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/update")
    public R<IsfdModelManagementPartVo> update(@Validated(EditGroup.class) @RequestBody IsfdModelManagementUpdateImportPartBo bo) {
        return R.ok(isfdModelManagementPartService.update(bo));
    }


    /**
     * 整机导入-确认导入
     */
    @SaCheckPermission("isfd:modelManagementPart:confirm:import")
    @Log(title = "整机导入-确认导入", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/confirm/import")
    public R<IsfdModelManagementConfirmImportPartVo> confirmImport(@Validated(AddGroup.class) @RequestBody IsfdModelManagementPartConfirmImportBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        IsfdModelManagementConfirmImportPartVo vo = isfdModelManagementPartService.confirmImportPartitions(bo, loginUser);
        return R.ok(vo);
    }


    /**
     * 导入取消
     */
    @SaCheckPermission("isfd:modelManagementPart:cancel:import")
    @Log(title = "导入取消", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @PostMapping("/cancel/import")
    public R<Void> cancelImport(@Validated(AddGroup.class) @RequestBody List<Long> ids) {
        return toAjax(isfdModelManagementPartService.cancelImport(ids));
    }

    /**
     * 导出模型零组件清单
     */
    @SaCheckPermission("isfd:modelManagementPart:bom:export")
    @Log(title = "导出模型零组件清单", businessType = BusinessType.EXPORT)
    @GetMapping("/bom/export")
    public void bomExport(@NotNull(message = "机型编号不能为空") @RequestParam("modelNumber") String modelNumber, HttpServletResponse response) {
        isfdModelManagementPartService.bomExport(modelNumber, response);
    }


    /**
     * 单个零件导入
     */
    @SaCheckPermission("isfd:modelManagementPart:single:import")
    @Log(title = "单个零件导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/single/import")
    public R<IsfdModelManagementPartVo> singleImport(@RequestBody IsfdModelManagementPartBo partBo) {
        IsfdModelManagementPartVo vo = isfdModelManagementPartService.singleImport(partBo);
        return R.ok(vo);
    }

    /**
     * 单个零件导入-规则检查
     */
    @SaCheckPermission("isfd:modelManagementPart:single:import")
    @Log(title = "单个零件导入-规则检查", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/check/single/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<IsfdModelManagementPartVo> singleImportCheck(@RequestParam("file") MultipartFile file,
                                     @NotEmpty(message = "所选机型编号不能为空") @RequestParam("modelNumber") String modelNumber,
                                     @NotEmpty(message = "所选批次编号不能为空") @RequestParam("batchNumber") String batchNumber,
                                     @NotEmpty(message = "所选部段编号不能为空") @RequestParam("sectionNumber") String sectionNumber,
                                     @NotEmpty(message = "所选组件编号不能为空") @RequestParam("componentNumber") String componentNumber) {
        return R.ok(isfdModelManagementPartService.checkSingleImport(file, modelNumber, batchNumber, sectionNumber, componentNumber));
    }

    /**
     * 单个零件版本删除
     */
    @SaCheckPermission("isfd:modelManagementPart:delete")
    @Log(title = "单个零件删除", businessType = BusinessType.DELETE)
    @PostMapping(value = "/delete")
    public R<String> deletePartVersion(@RequestParam("id") Long id) {
        String massage = isfdModelManagementPartService.deletePartById(id);
        return R.ok(massage);
    }


    /**
     * 组件导入-规则检查
     */
    @SaCheckPermission("isfd:modelManagementPart:check:import")
    @Log(title = "组件导入-规则检查", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/component/check/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<List<IsfdModelManagementPartVo>> componentCheckImport(@RequestParam("file") MultipartFile file,
                                                                   @NotEmpty(message = "所选机型编号不能为空") @RequestParam("modelNumber") String modelNumber,
                                                                   @NotEmpty(message = "所选批次编号不能为空") @RequestParam("batchNumber") String batchNumber,
                                                                   @NotEmpty(message = "所选部段编号不能为空") @RequestParam("sectionNumber") String sectionNumber) {
        List<IsfdModelManagementPartVo> voList = isfdModelManagementPartService.componentCheckImport(file, modelNumber, batchNumber, sectionNumber);
        return R.ok(voList);
    }

    /**
     * 组件导入-确认导入
     */
    @SaCheckPermission("isfd:modelManagementPart:confirm:import")
    @Log(title = "组件导入-确认导入", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/component/confirm/import")
    public R<IsfdModelManagementConfirmImportPartVo> componentConfirmImport(@Validated(AddGroup.class) @RequestBody List<IsfdModelManagementPartBo> boList) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        IsfdModelManagementConfirmImportPartVo vo = isfdModelManagementPartService.componentConfirmImport(boList, loginUser);
        return R.ok(vo);
    }
}
